
# Code for Table S1: Timing

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

library('AER')
library('ivpack')
library('dplyr')
library('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

## ---------------------------------------
## Row 1: Excluding cases within 0 months
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812)
m1a2 <- ivreg(form.2, data = data0812)
m1a3 <- ivreg(form.3, data = data0812)

row1 <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(mean(data0812$incar2, na.rm=T), NA, NA),
                               c(mean(data0812$incar1, na.rm=T), NA, NA),
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))

## ---------------------------------------
## Row 2: Excluding cases within 2 months
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812[bail_date <= "2012-09-06", ])
m1a2 <- ivreg(form.2, data = data0812[bail_date <= "2012-09-06", ])
m1a3 <- ivreg(form.3, data = data0812[bail_date <= "2012-09-06", ])

row2 <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(mean(data0812[bail_date <= "2012-09-06", incar2], na.rm=T), NA, NA),
                               c(mean(data0812[bail_date <= "2012-09-06", incar1], na.rm=T), NA, NA),
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))
mean(data0812[bail_date <= "2012-09-06", incar2], na.rm=T)

## ---------------------------------------
## Row 3: Excluding cases within 4 months
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812[bail_date <= "2012-07-06", ])
m1a2 <- ivreg(form.2, data = data0812[bail_date <= "2012-07-06", ])
m1a3 <- ivreg(form.3, data = data0812[bail_date <= "2012-07-06", ])


row3 <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(mean(data0812[bail_date <= "2012-07-06", incar2], na.rm=T), NA, NA),
                               c(mean(data0812[bail_date <= "2012-07-06", incar1], na.rm=T), NA, NA),
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))


## ---------------------------------------
## Row 4: Excluding cases within 6 months
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812[bail_date <= "2012-05-06", ])
m1a2 <- ivreg(form.2, data = data0812[bail_date <= "2012-05-06", ])
m1a3 <- ivreg(form.3, data = data0812[bail_date <= "2012-05-06", ])

row4 <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(mean(data0812[bail_date <= "2012-05-06", incar2], na.rm=T), NA, NA),
                               c(mean(data0812[bail_date <= "2012-05-06", incar1], na.rm=T), NA, NA),                           
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))


## ---------------------------------------
## Row 5: Excluding cases within 12 months
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812[bail_date <= "2011-11-06", ])
m1a2 <- ivreg(form.2, data = data0812[bail_date <= "2011-11-06", ])
m1a3 <- ivreg(form.3, data = data0812[bail_date <= "2011-11-06", ])


row5 <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(mean(data0812[bail_date <= "2011-11-06", incar2], na.rm=T), NA, NA),
                               c(mean(data0812[bail_date <= "2011-11-06", incar1], na.rm=T), NA, NA),
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))


cont <- rbind(row1, row2, row3, row4, row5)

colnames(cont) <- c("(1)", "(2)", "(3)")
# add row labels
cont$names <-  c("Pretrial Incarceration (PTI) - 0 months",
                 "Std. Error",
                 "Prop. detained pretrial during election", 
                 "Prop. incarcerated post-conviction during election",
                 "Obs",
                 "Pretrial Incarceration (PTI) - 2 months",
                 "Std. Error",
                 "Prop. detained pretrial during election", 
                 "Prop. incarcerated post-conviction during election",
                 "Obs",
                 "Pretrial Incarceration (PTI) - 4 months",
                 "Std. Error",
                 "Prop. detained pretrial during election", 
                 "Prop. incarcerated post-conviction during election",
                 "Obs",
                 "Pretrial Incarceration (PTI) - 6 months",
                 "Std. Error",
                 "Prop. detained pretrial during election", 
                 "Prop. incarcerated post-conviction during election",
                 "Obs",
                 "Pretrial Incarceration (PTI) - 12 months",
                 "Std. Error",
                 "Prop. detained pretrial during election", 
                 "Prop. incarcerated post-conviction during election",
                 "Obs"
                 )
cont <- cont[, c("names", "(1)", "(2)", "(3)")]
cat("\nPrinting Table S1: Timing...\n")
print(cont)


